﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class Sales_SalesManage : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Hidid.Value = Request.Cookies["user"].Values["id"];
            HidWareid.Value = getWareId();

            if (HidWareid.Value != "0")
            {
                ddlWare.Visible = false;
                lblWare.Visible = false;
            }
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);


            initGvData();
            initWare();
            //this.GvData.Columns[0].Visible = false;
            // btnAdd.Enabled = false;

        }
    }

    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Hidid.Value;
        zs.Open(sql);
        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }
    private void initGvData()
    {
        string sql = "select stockid,e.Brand,k.WarehouseName,f.ComoditiesName,i.Specifications,j.Units,a.comodid,ComodBarcode,SalesPrices,totalNum from CS_Stock a join Brand e on a.brandid=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.modelid=i.SpecificationsId join Units j on a.Unitid=j.UnitsId join CS_Warehouse k on a.WarehouseId=k.WarehouseId";
        if (HidWareid.Value == "0")
        {
            //zs.Open();
            PageNavigator1.Total = 1;
            PageNavigator1.Query = sql;
        }
        else
        {
            sql += " where a.WarehouseId=" + HidWareid.Value;
            PageNavigator1.Total = 1;
            PageNavigator1.Query = sql;

            //zs.Open("select stockid,e.Brand,k.WarehouseName,f.ComoditiesName,i.Specifications,j.Units,a.comodid,ComodBarcode,SalesPrices,totalNum from CS_Stock a join Brand e on a.brandid=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.modelid=i.SpecificationsId join Units j on a.Unitid=j.UnitsId join CS_Warehouse k on a.WarehouseId=k.WarehouseId );
        }

        //GvData.DataSource = zs.m_table;
        //GvData.DataBind();
    }



    protected void btnSearch_Click(object sender, EventArgs e)
    {


        string sql = "select stockid,e.Brand,k.WarehouseName,f.ComoditiesName,i.Specifications,j.Units,a.comodid,ComodBarcode,SalesPrices,totalNum from CS_Stock a join Brand e on a.brandid=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.modelid=i.SpecificationsId join Units j on a.Unitid=j.UnitsId join CS_Warehouse k on a.WarehouseId=k.WarehouseId where 1=1";

        if (!string.IsNullOrEmpty(txtCode.Text))
        {
            sql += " and a.Comodid='" + Common.FormatParameter(txtCode.Text) + "'";
        }
        if (!string.IsNullOrEmpty(txtBrand.Text))
        {
            sql += " and e.Brand='" + Common.FormatParameter(txtBrand.Text) + "'";
        }
        if (!string.IsNullOrEmpty(txtContent.Text))
        {
            sql += " and f.ComoditiesName='" + Common.FormatParameter(txtContent.Text) + "'";
        }
        if (ddlWare.SelectedIndex != 0)
        {
            sql += " and a.WarehouseId=" + ddlWare.SelectedValue;
        }
        if (HidWareid.Value != "0")
        {
            ddlWare.Visible = false;
            lblWare.Visible = false;
            sql += " and a.WarehouseId=" + HidWareid.Value;
        }

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;




    }

 

    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 10, GvData);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int tempFlowId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "totalNum"));

            if (tempFlowId < 20)
            {
                e.Row.Cells[7].ForeColor = Color.Red;
            }
        }
    }

    private void initWare()
    {
        ZSql zs = new ZSql();
        zs.Open("select WarehouseId,WarehouseName from CS_Warehouse");
        ddlWare.DataSource = zs.m_table;
        ddlWare.DataTextField = "WarehouseName";
        ddlWare.DataValueField = "WarehouseId";
        ddlWare.DataBind();
        ddlWare.Items.Insert(0, new ListItem("—选择仓库—", "0"));
        zs.Close();
    }
}
